﻿#include "financewidget.h"
#include "ui_financewidget.h"
#include "readonlydelegate.h"

FinanceWidget::FinanceWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FinanceWidget)
{
    ui->setupUi(this);
    relation = nullptr;
    relatedMessageId.clear();

    ui->tableView->setModel(&model);
    preSave = 0;

    for(int i=0;i<model.header.size()-1;i++){
        ui->tableView->setItemDelegateForColumn(i, new ReadOnlyDelegate());
    }

    connect(ui->lineEditClientName, &ClickedLineEdit::doubleClicked,
            [=]()
    {
        userPicker.loadData();
        userPicker.exec();
    });

    connect(ui->lineEditContact, &ClickedLineEdit::doubleClicked,
            [=]()
    {
        userPicker.loadData();
        userPicker.exec();
    });

    connect(ui->lineEditTel, &ClickedLineEdit::doubleClicked,
            [=]()
    {
        userPicker.loadData();
        userPicker.exec();
    });

    connect(&userPicker, &UserPicker::selectedFinished,
            [=](UserRelation *relation)
    {
        relatedMessageId.clear();
        ui->lineEditFinanceValue->clear();
        ui->lineEditFinanceValue->setEnabled(true);
        this->dealUserRelationSelected(relation);
    });

    connect(&dingdanService, &DingdanService::getDingdanDetailByTypeSuccess,
            [=](QList<DingdanAndDetail*> details)
    {
        model.reset();
        ui->tableView->refreshHiddenState();
        model.insertDingdanDetail(details);
    });

    connect(&dingdanService, &DingdanService::financeSubmitSuccess,
            [=]()
    {
        ui->lineEditFinanceValue->clear();
        ui->lineEditFinanceValue->setEnabled(true);
        relatedMessageId.clear();
        this->dingdanService.getDingdanDetails(
                    this,
                    widgetType,
                    relation->targetUsername);
    });

    connect(&messagePicker, &MessagePicker::importMessageSelected,
            [=](Message *message)
    {
        relatedMessageId = message->messageId;
        ui->lineEditFinanceValue->clear();
        ui->lineEditFinanceValue->setEnabled(false);
        this->relation = message->getUserRelation();
        double financeValue = 0;
        if(widgetType == Dingdan::TicketIn){
            financeValue = message->ticketOut;
        }else if(widgetType == Dingdan::MoneyIn){
            financeValue = message->moneyOut;
        }
        ui->lineEditFinanceValue->setText(QString::number(financeValue, 'f', 2));
        this->dealUserRelationSelected(relation);
    });
}

FinanceWidget::~FinanceWidget()
{
    delete ui;
}

void FinanceWidget::setWidgetType(Dingdan::FinanceWidgetType widgetType){
    this->widgetType = widgetType;
    model.setWidgetType(widgetType);
    ui->tableView->setListPushButtonAndMenuLabel(ui->pushButtonList, model.header);
    int headerSize = model.header.size();
    QString typeName;
    switch (widgetType) {
    case Dingdan::TicketOut:
        ui->labelClientName->setText("客户名称：");
        ui->labelPreSave->setText("预开发票：");
        userPicker.setIsShowPlatformUser(true);
        userPicker.setWidgetType(UserRelation::Client);
        ui->tableView->initHiddenStateList("finance/ticketOut", headerSize);
        typeName = "开票";
        ui->labelFinanceTotal->setText("应开发票总额:0");
        ui->pushButtonImport->setVisible(false);
        connect(&model, &FinanceStandardItemModel::updateTotal,
                [=](double totalFinance)
        {
            ui->labelFinanceTotal->setText(QString("应开发票总额:%1").arg(totalFinance));
        });
        break;
    case Dingdan::MoneyIn:
        ui->labelClientName->setText("客户名称：");
        ui->labelPreSave->setText("预收货款：");
        userPicker.setIsShowPlatformUser(false);
        userPicker.setWidgetType(UserRelation::Client);
        ui->tableView->initHiddenStateList("finance/MoneyIn", headerSize);
        typeName = "收款";
        ui->labelFinanceTotal->setText("应收货款总额:0");
        ui->pushButtonImport->setVisible(true);
        messagePicker.setWidgetType(MessagePickerStandardItemModel::MoneyIn);
        connect(&model, &FinanceStandardItemModel::updateTotal,
                [=](double totalFinance)
        {
            ui->labelFinanceTotal->setText(QString("应收货款总额:%1").arg(totalFinance));
        });
        break;
    case Dingdan::TicketIn:
        ui->labelClientName->setText("供应商名称：");
        ui->labelPreSave->setText("预收发票：");
        userPicker.setIsShowPlatformUser(false);
        userPicker.setWidgetType(UserRelation::Giver);
        ui->tableView->initHiddenStateList("finance/ticketIn", headerSize);
        typeName= "收票";
        ui->labelFinanceTotal->setText("应收发票总额:0");
        ui->pushButtonImport->setVisible(true);
        messagePicker.setWidgetType(MessagePickerStandardItemModel::TicketIn);
        connect(&model, &FinanceStandardItemModel::updateTotal,
                [=](double totalFinance)
        {
            ui->labelFinanceTotal->setText(QString("应收发票总额:%1").arg(totalFinance));
        });
        break;
    case Dingdan::MoneyOut:
        ui->labelClientName->setText("供应商名称：");
        ui->labelPreSave->setText("预付货款：");
        userPicker.setIsShowPlatformUser(true);
        userPicker.setWidgetType(UserRelation::Giver);
        ui->tableView->initHiddenStateList("finance/moneyOut", headerSize);
        typeName = "付款";
        ui->labelFinanceTotal->setText("应付货款总额:0");
        ui->pushButtonImport->setVisible(false);
        connect(&model, &FinanceStandardItemModel::updateTotal,
                [=](double totalFinance)
        {
            ui->labelFinanceTotal->setText(QString("应付货款总额:%1").arg(totalFinance));
        });
        break;
    }

    ui->labelTypeName->setText(typeName);
    ui->pushButtonSubmit->setText(typeName);
    ui->labelFinanceValue->setText(typeName+"金额");
}

void FinanceWidget::dealUserRelationSelected(UserRelation *relation){
    this->relation = relation;
    ui->lineEditClientName->setText(relation->nickName);
    ui->lineEditContact->setText(relation->contact);
    ui->lineEditTel->setText(relation->tel);

    switch (widgetType) {
    case Dingdan::TicketOut:
        preSave = relation->preTicketOut;
        break;
        case Dingdan::MoneyIn:
        preSave = relation->preMoneyIn;
        break;
    case Dingdan::TicketIn:
        preSave = relation->preTicketIn;
        break;
    case Dingdan::MoneyOut:
        preSave = relation->preMoneyOut;
        break;
    }

    ui->lineEditPreSave->setText(QString("%1").arg(preSave));
    this->dingdanService.getDingdanDetails(
                this,
                widgetType,
                relation->targetUsername);
}

void FinanceWidget::on_pushButtonFinanceAllocate_clicked()
{
    double totalFinance = ui->lineEditFinanceValue->text().toDouble();
    totalFinance += preSave;
    model.financeAllocate(totalFinance);
}

void FinanceWidget::on_lineEditFinanceValue_textChanged(const QString &text)
{
    double financeValue = text.toDouble();
    financeValue = financeValue<0?0:financeValue;
    ui->lineEditFinanceValue->setText(QString("%1").arg(financeValue));
}

void FinanceWidget::on_pushButtonSubmit_clicked()
{
    if(!userService.inputOperatePassword(this)){
        return;
    }

    double totalFinanceGive = ui->lineEditFinanceValue->text().toDouble();
    dingdanService.financeSubmit(
                this,
                ui->pushButtonSubmit,
                widgetType,
                relatedMessageId,
                relation,
                preSave,
                totalFinanceGive,
                model.totalFinanceMake(),
                model.totalFinanceNeed(),
                model.validDingdanDetail());
}

void FinanceWidget::on_pushButtonClear_clicked()
{
    this->relation = nullptr;
    this->preSave = 0;
    ui->lineEditClientName->clear();
    ui->lineEditContact->clear();
    ui->lineEditTel->clear();
    ui->lineEditPreSave->setText("0");
    ui->lineEditFinanceValue->setText("0");
    model.reset();
}

void FinanceWidget::on_pushButtonImport_clicked()
{
    messagePicker.showAndInit(ui->pushButtonImport);
}
